package cn.xerllent.wxtp.service.impl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.hutool.core.util.ObjectUtil;
import cn.xerllent.common.entity.vo.OffsetPageable;
import cn.xerllent.common.service.impl.BaseServiceImpl;
import cn.xerllent.wxtp.entity.Dksb;
import cn.xerllent.wxtp.entity.DksbCourse;
import cn.xerllent.wxtp.service.DksbCourseService;
import cn.xerllent.wxtp.service.dto.DksbFilter;
import cn.xerllent.wxtp.service.mapper.DksbCourseMapper;
import cn.xerllent.common.utils.FileUtil;

@Service
//@AllArgsConstructor
//@CacheConfig(cacheNames = "user")
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class DksbCourseServiceImpl extends BaseServiceImpl<DksbCourseMapper, DksbCourse>  implements DksbCourseService {

    @Override
    //@Cacheable
    public List<DksbCourse> queryAll(DksbFilter filter, OffsetPageable offsetPageable) {
    	PageHelper.offsetPage(offsetPageable.getOffset(), offsetPageable.getPageSize(), false);//false 表示不需要count查询
        PageInfo<DksbCourse> page = new PageInfo<>(queryAll(filter));
        return page.getList();
    }

    @Override
    //@Cacheable
    public List<DksbCourse> queryAll(DksbFilter filter) {
    	return baseMapper.queryWithDksb(filter);
    }

    @Override
    public void download(List<DksbCourse> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (DksbCourse Dksb : all) {
            Map<String, Object> map = new LinkedHashMap<>();
            map.put("id", Dksb.getId());
            map.put("申报id", Dksb.getDksbId());
            map.put("被代课教师", Dksb.getSrcUserid());
            map.put("代课教师", Dksb.getTarUserid());
            map.put("代课日期", Dksb.getEventdate());
            map.put("年级", Dksb.getGrade());
            map.put("班级", Dksb.getClasz());
            map.put("学科", Dksb.getSubject());
            map.put("节次", Dksb.getCoursetime());
            map.put("课时数", Dksb.getHours());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
    
	/**
	 * 清除对应Mapper的二级缓存，采用无害删除空数据模拟清空,因为二级缓存更新机制是本表数据更新即可清空<br>
	 * 提供本方法供联表更新查询的实时更新
	 * */
	@Override
	public void clearCache() {
		baseMapper.deleteById(0);
	}
	
	/**
	 * 检查课程申报数据是否重复,
	 * 0-未提交；1-审批中；2-已通过；3-已驳回；4-已取消
	 * */
	@Override
	public DksbCourse checkRepeat(String corpId, DksbCourse course) {
		return this.baseMapper.checkRepeat(corpId, course);
	}

}
